package controller;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import domain.Address;
import domain.itf.IDAOAddress;

/**
 * Controller class called by ajax to get all address
 */
@Controller
@RequestMapping("/GetAllAddress")
public class GetAllAddress {


	@Autowired
	ServletContext servletContext;

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) {
		
		// get the IDAOContact with spring
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		IDAOAddress daoAddress =(IDAOAddress) context.getBean("DAOAddress");

		List<Address> setAddress= daoAddress.getAllAddress();
		if(setAddress.size() == 0)
			setAddress = null;
		ModelAndView view = new ModelAndView("listAddress");
		
		view.addObject("address", setAddress);

		return view;
	}

}
